<?php

namespace app\api\logic;


use app\api\Request;
use app\common\service\UserService;

class ExamTime
{
    /**
     * getUserExamTime
     * @throws
     */
    public function getIndexExamData()
    {
        // 配置信息
        $result = [
            'show' => false,
            'days' => 0,
        ];

        // 提取参数
        $userId = app(Request::class)->param('userId');
        if (!$userId) {
            return $result;
        }

        // 查询用户信息
        $userService = new UserService();
        $userInfo = $userService->getUserById($userId, '*');

        // 未登录 | 无考试信息 | 无省份 不显示报考信息
        if (!$userInfo || !$userInfo['exam_id'] || !$userInfo['region_id']) {
            return $result;
        }

        // 匹配考试时间
        $examTimeRow = app(\app\common\model\ExamTime::class)->matchExamTimeRecord($userInfo['exam_id'], $userInfo['region_id'], $userInfo['profession_id']);
        if ($examTimeRow) {
            // 计算时间差,转换为天数
            $timeDiff = $examTimeRow['exam_time'] - time();
            $result['show'] = true;
            $result['days'] = floor($timeDiff / 86400);
        }

        // 返回
        return $result;
    }
}